/**
 * Created by huaicheng151201@163.com on 2017/5/22.
 * summary :
 */
(function () {
    if(!jQuery) return;
    if ('addEventListener' in document) {
        document.addEventListener('load', function() {
            FastClick && FastClick.attach(document.body);
        }, false);
    }
    var startX, startY, moveEndX, moveEndY, X, Y, critical= 10;
    var touchstart= function(e){
        e.preventDefault();
        startX = (e.clientX || e.originalEvent.changedTouches[0].pageX),
        startY = (e.clientY || e.originalEvent.changedTouches[0].pageY);
    }
    var touchmove= function(e){
        e.preventDefault();
        moveEndX = (e.clientX || e.originalEvent.changedTouches[0].pageX),
        moveEndY = (e.clientY || e.originalEvent.changedTouches[0].pageY);
    }
    var touchend= function(e, direction, fun, $dom){
        e.preventDefault();
        X = moveEndX - startX,  Y = moveEndY - startY;
        if ( X > critical ) {
            if(direction== "r") fun.call($dom, {
                offsetX : X,
                offsetY : Y
            });
        }
        else if ( X < -critical ) {
            if(direction== "l") fun.call($dom, {
                offsetX : X,
                offsetY : Y
            });
        }
        if ( Y > critical) {
            if(direction== "b") fun.call($dom, {
                offsetX : X,
                offsetY : Y
            });
        }
        else if ( Y < -critical ) {
            if(direction== "t") fun.call($dom, {
                offsetX : X,
                offsetY : Y
            });
        }
        else{
            //alert("just touch");
        }
    }
    var moveFun= function($dom, fun, direction){
        if(!jQuery.isFunction(fun)){
            return;
        }
        var ismove= false;
        //PC
        $dom.mousedown(function(e){
            touchstart(e);
            ismove= true;
        }).mousemove(function(e){
            if(!ismove) return;
            touchmove(e);
        }).mouseup(function(e){
            ismove= false;
            touchend(e, direction, fun, $dom);
        })
        //mobile
        $dom.on("touchstart", function (e) {
            touchstart(e);
            ismove= true;
        }).on("touchmove", function (e) {
            if(!ismove) return;
            touchmove(e);
        }).on("touchend", function (e) {
            ismove= false;
            touchend(e, direction, fun, $dom);
        })
    }

    $.fn.dmMouseDown = function (fun) {
        if(!jQuery.isFunction(fun)){
            return;
        }
        this.mousedown(fun);
        this.on("touchstart", fun)
    }
    $.fn.dmMouseMove = function(fun){
        if(!jQuery.isFunction(fun)){
            return;
        }
        this.mousemove(fun);
        this.on("touchmove", fun)
    }
    $.fn.dmMouseMoveLeft = function (fun) {
        moveFun(this, fun, "l");
    }
    $.fn.dmMouseMoveRight = function (fun) {
        moveFun(this, fun, "r");
    }
    $.fn.dmMouseMoveTop = function (fun) {
        moveFun(this, fun, "t");
    }
    $.fn.dmMouseMoveDown = function (fun) {
        moveFun(this, fun, "b");
    }
    $.fn.dmMouseUp = function (fun) {
        if(!jQuery.isFunction(fun)){
            return;
        }
        this.mouseup(fun);
        this.on("touchend", fun);
    }
    $.fn.dmClick = function (fun) {
        if(!jQuery.isFunction(fun)){
            return;
        }
        this.click(fun);
        this.on("touchend", fun);
    }
    $.fn.dmDblClick = function (fun) {
        if(!jQuery.isFunction(fun)){
            return;
        }
        this.dblclick(fun);
    }
})()
